<?php 
session_cache_expire(999999999);
if(!isset($_SESSION)) 
	{ 
		session_start(); 
	}
include 'operations/connection.php';
include "generallibraries/funcs.php";
require_once('calendar/classes/tc_calendar.php');
$_SESSION['action'] = "acc-search-searchpurchase";

if(!isset($_REQUEST['page'])){
	$list["approvedoc.Tile"]="ASC";
	$list["employee.FullName"]="ASC";
	$list["approvedoc.SumMoney"]="ASC";
	$list["approvedoc.Timecreate"]="DESC";
	$str="approvedoc.Timecreate";
	$_SESSION['sortn']=$str;
	$_SESSION['sort']=$list;
}else{
	$str=$_SESSION['sortn'];
	$list=$_SESSION['sort'];
}

if(array_key_exists("page",$_GET))
    $page  = $_GET['page'];
else
    $page = 1;
    
//$hamsort=" where ac_customer.Activest=1 order by ac_customer.CreatedDate desc";
//$sqlCustomer = generalFunctions::listCustomer("ac_customer.CustomerID, ac_customer.CustomerName",$hamsort);
$sqlCustomer="select DISTINCT buying_document.company CustomerName 
from approvedoc,buying_document 
where approvedoc.IDTypeDoc in(13,15) 
and approvedoc.IDApproveDoc=buying_document.document_id";
$mangCustomer=mysql_query($sqlCustomer);

    if(isset($_REQUEST['Search'])){
    	$fromdate=$_REQUEST['datefrom'];
		$todate=$_REQUEST['dateto'];
		$cus=$_REQUEST['cbcus'];
		$search=$_REQUEST['Search'];
    	$sql="select DISTINCT approvedoc.IDApproveDoc, approvedoc.IDTypeDoc, approvedoc.Tile, employee.FullName, approvedoc.Timecreate, approvedoc.SumMoney 
    	from approvedoc ,buying_document,employee 
    	where approvedoc.IDEmployee=employee.IDEmployee 
    	and approvedoc.IDTypeDoc in(13,15) and approvedoc.IDStatus=1 
    	and approvedoc.IDApproveDoc=buying_document.document_id  ";
    	if($fromdate!="" && $todate!=""){
    		$sql .="AND (approvedoc.Timecreate > '".$fromdate." 00:00:00' AND approvedoc.Timecreate < '".$todate." 23:59:59') ";
    	}else{
    		if($fromdate!="" ){
    			$sql .=" and approvedoc.Timecreate > '".$fromdate." 00:00:00' ";
    		}else if($todate!=""){
    			$sql .=" AND approvedoc.Timecreate < '".$todate." 23:59:59' ";
    		}
    	}
    	
    	if(isset($cus)){
			$sql .=" and buying_document.company like '%".$cus."%' ";
		}
		$sql .=" ORDER BY ".$str." ".$list[$str];
    	$mapp=mysql_query($sql);
		$number_of_page = ceil ( mysql_num_rows( $mapp ) / $rows_per_page );
    }else if(isset($_REQUEST['SearchALL'])){
    	$_REQUEST['cbcus']="";
		$_REQUEST['datefrom']="";
		$_REQUEST['dateto']="";
		$search=$_REQUEST['SearchALL'];
		//select DISTINCT buying_document.company from buying_document where buying_document.company!=''
		$sql="select DISTINCT approvedoc.IDApproveDoc, approvedoc.IDTypeDoc, approvedoc.Tile, employee.FullName, approvedoc.Timecreate, approvedoc.SumMoney 
		from approvedoc ,buying_document, employee 
		where approvedoc.IDEmployee=employee.IDEmployee 
		and approvedoc.IDTypeDoc in(13,15) and approvedoc.IDStatus=1 
		and approvedoc.IDApproveDoc=buying_document.document_id  ORDER BY ".$str." ".$list[$str];
		$mapp=mysql_query($sql);
		$number_of_page = ceil ( mysql_num_rows( $mapp ) / $rows_per_page );
    }else{
    	$fromdate=date("Y-m-d");
		$todate=date("Y-m-d");
		$_REQUEST['cbcus']="";
		$_REQUEST['datefrom']=$fromdate;
		$_REQUEST['dateto']=$todate;
		//select DISTINCT buying_document.company from buying_document where buying_document.company!=''
		$sql="select DISTINCT approvedoc.IDApproveDoc, approvedoc.IDTypeDoc, approvedoc.Tile, employee.FullName, approvedoc.Timecreate, approvedoc.SumMoney 
		from approvedoc ,buying_document,employee 
		where approvedoc.IDEmployee=employee.IDEmployee 
		and approvedoc.IDTypeDoc in(13,15) and approvedoc.IDStatus=1 
		and approvedoc.IDApproveDoc=buying_document.document_id AND (approvedoc.Timecreate > '".$fromdate." 00:00:00' AND approvedoc.Timecreate < '".$todate." 23:59:59') 
		ORDER BY ".$str." ".$list[$str];
		$mapp=mysql_query($sql);
		$number_of_page = ceil ( mysql_num_rows( $mapp ) / $rows_per_page );
    }
?>
<link	href="css/style.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" href="js/jquery.autocomplete.css" />
<script type="text/javascript" src="js/jquery.autocomplete.js"></script>
<style type="text/css">
#content_menu_expand{
	position:absolute; 
	right:-5px;
	top:20px;
	z-index:999;
	background-color: #ffffff;
	-moz-border-radius: 7px;
	-webkit-border-radius: 7px;
	color: #000000;
	width:300px; 
	max-height:180px;
	overflow:auto;
}
.passul{padding-left:0; border:1px solid black}
.bgkhac {background: #eee;}
.passul ul{padding:0px}
.passul ul li {color:#474747; font-size:12px;padding: 5px 0;padding-left:5px;list-style: none;}
.passul ul li:hover { background: #0A246A;color:white;text-decoration: none;list-style: none;}
</style>
<script type="text/javascript">
$(document).ready(function(){
	$("#inputString").autocomplete("operations/rpc_searchcus.php", {
		selectFirst: true
	});
	$('#content_menu_expand').hide();
	$(document).click(function() {
		$("#content_menu_expand").hide();
	});
	$('#content_menu_expand').click(function(e) {
		e.stopPropagation();
	});

	$('#menu_expand').click(function() {
		if ($('#content_menu_expand').is(":hidden")) {
			$('#content_menu_expand').show();
		}
		else {
			$('#content_menu_expand').hide();
		}
		return false;
	});
	$('#close_menu').click(function() {
		$('#content_menu_expand').hide();
		return false;
	});

	$("ul.menu_inside li a").hover(function() {
		$(this).parent().find("ul.subnav").slideDown(0).show();
		$(this).parent().hover(function() {
		}, function(){
			$(this).parent().find("ul.subnav").slideUp(0);
		});
	}).hover(function() {
		$(this).addClass("subhover");
	}, function(){
		$(this).removeClass("subhover");
	});
});
/*function xlsearch()
{
	if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}else{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function(){
										  if (xmlhttp.readyState==4 && xmlhttp.status==200){
										    	document.getElementById("viewsearch").innerHTML=xmlhttp.responseText;
										  }
									  }
	sfromdate=document.getElementById("datefrom").value;
	stodate=document.getElementById("dateto").value;
	var setax = document.getElementById("cbcus");
	var scbcus = setax.options[setax.selectedIndex].value;
	xmlhttp.open("GET","ajax/acc/searchpurchase.php?search=Search&datefrom="+sfromdate+"&dateto="+stodate+"&cbcus="+scbcus,true);
	xmlhttp.send();
}*/
function sort(page,str,co,s)
{
	sfromdate=document.getElementById("datefrom").value;
	stodate=document.getElementById("dateto").value;
	//var setax = document.getElementById("cbcus");
	//var scbcus = setax.options[setax.selectedIndex].value;
	var scbcus = document.getElementById("inputString").value;
	//alert(sfromdate+" --- "+stodate+" --- "+scbcus+" -- "+s);
	if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}else{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function(){
										  if (xmlhttp.readyState==4 && xmlhttp.status==200){
										    	document.getElementById("viewsearch").innerHTML=xmlhttp.responseText;
										    	resetvalue();
										  }
									  }
	xmlhttp.open("GET","ajax/acc/sortsearchpurchase.php?datefrom="+sfromdate+"&dateto="+stodate+"&cbcus="+encodeURIComponent(scbcus)+"&page="+page+"&str="+str+"&co="+co+"&search="+s,true);
	xmlhttp.send();
}

function clickCustomer(gt){
	document.getElementById("inputString").value=gt;
	$('#content_menu_expand').hide();
}
</script>
 <link rel="stylesheet" type="text/css" href="admin/css/jsDatePick_ltr.min.css" />

<script type="text/javascript" src="admin/css/jsDatePick.min.1.3.js"></script>

<script type="text/javascript">

window.onload = function(){

new JsDatePick({

useMode:2,

target:"datefrom",
dateFormat:"%Y-%m-%d"

});

new JsDatePick({

	useMode:2,

	target:"dateto",
	dateFormat:"%Y-%m-%d"

	});
};

</script>

<div class="title"></div>
                    <div class="content_code">
                    	<form action="accountant-search.php" method="get" name="fspurchase">
                    	<input type="hidden" name="content" value="accountant-search-SearchPurchase-content" />
						<div class="approval-right">		
                        	<div style="font-size:18px; padding-left:30px; margin-bottom:10px; font-weight:bold; background:url(images/find.png) no-repeat left center"> Search</div>				
							<div style="width:820px;border: 1px solid silver; padding: 10px 0; margin-bottom: 30px; background:white">
                                <table width="820px" style="padding:0 10px">
                                	<tr>
                                    	<td style="width:120px"><?php echo $l_from;?></td>
                                        <td style="width:120px"><?php echo $l_to;?></td>
                                        <td style="width:320px"><?php echo $l_customer;?></td>
                                        <td style="width:130px;"></td>
                                        <td style="width:130px;"></td>
                                    </tr>
                                    <tr>
                                        <td style="width:120px">
                                        	<?php
											 /* $myCalendar1 = new tc_calendar("datefrom", true);
											  $myCalendar1->setIcon("calendar/images/iconCalendar.gif");
											  $d=split('-',$fromdate,3);
											 
											  $myCalendar1->setDate($d[2],$d[1],$d[0]);
											  $myCalendar1->setPath("calendar/");
											  $myCalendar1->setYearInterval(2010, 2015);
											  $myCalendar1->dateAllow('2010-01-01', '2015-12-31');
											  //$myCalendar1->setHeight(350);
											  //$myCalendar1->autoSubmit(true, "form1");
											  //$myCalendar1->setSpecificDate(array("2011-04-01", "2011-04-13", "2011-04-25"), 0, 'month');
											  $myCalendar1->setOnChange("myChanged('test')");
											  //$myCalendar->rtl = true;
											  $myCalendar1->writeScript();
						
											  //echo $myCalendar1->getDate();*/
											  
										  	?>
										  	<input type="text" style="height: 19px;" id="datefrom" name="datefrom" readonly="readonly" value="<?php echo $_REQUEST['datefrom'];?>" />
                                        </td>
                                        <td style="width:120px">
                                        	<?php
											/*  $myCalendar1 = new tc_calendar("dateto", true);
											  $myCalendar1->setIcon("calendar/images/iconCalendar.gif");
											  $d=split('-',$todate,3);
											 
											  $myCalendar1->setDate($d[2],$d[1],$d[0]);
											  $myCalendar1->setPath("calendar/");
											  $myCalendar1->setYearInterval(2010, 2015);
											  $myCalendar1->dateAllow('2010-01-01', '2015-12-31');
											  //$myCalendar1->setHeight(350);
											  //$myCalendar1->autoSubmit(true, "form1");
											  //$myCalendar1->setSpecificDate(array("2011-04-01", "2011-04-13", "2011-04-25"), 0, 'month');
											  $myCalendar1->setOnChange("myChanged('test')");
											  //$myCalendar->rtl = true;
											  $myCalendar1->writeScript();
						
											  //echo $myCalendar1->getDate();*/
											  
										  	?>
										  	<input type="text" id="dateto" style="height: 19px;" name="dateto" readonly="readonly" value="<?php echo $_REQUEST['dateto'];?>" />
                                        </td>
                                        
                                        <td style="width:320px">
                                        	<div style="position:relative; ">
											<div style="float:left;">
												<input  type="text" size="30" value="<?=$_REQUEST['cbcus']?>" name="cbcus" id="inputString"  style="height: 22px; width: 300px; border: 1px solid #c4c0c9;font-size: 12px;" autocomplete="off"; />
												<div class="suggestionsBox" id="suggestions" style="display:none;">
													<img src="images/upArrow.png" style="position: relative; top: -12px; left: 30px;" alt="upArrow" />
													<div class="suggestionList" id="autoSuggestionsList">
														&nbsp;
													</div>
												</div> 						
											</div>
											<div style=" float:left; position:relative; margin-top: 4px; margin-left: -21px; z-index:999">
												<img alt="" src="images/arrow-down.png" id="menu_expand" >
											    <div id="content_menu_expand" class="passul" style="display:none;">
											        <ul>
											        <?php 
											    		$sql="select DISTINCT buying_document.company CustomerName 
														from approvedoc,buying_document 
														where approvedoc.IDTypeDoc in(13,15) 
														and approvedoc.IDApproveDoc=buying_document.document_id";
														$result = mysql_query($sql);
														if($result)
														{
															$i=0;
															while($row=mysql_fetch_array($result))
															{
																?>
																<li onmouseover="this.style.cursor='pointer'"  onmouseout="this.style.cursor='default'" onclick="clickCustomer('<?=$row['CustomerName']?>')" 
																	<?php 
																		if($i%2!=0){
																			?>
																			class="bgkhac"
																			<?php 
																		}
																	?>
																> <?php echo $row['CustomerName'];?></li>
																<?php 
																$i++;
															}
														}
											    	?>
											        </ul>
											    </div>
											</div>
											</div>
                                        </td>
                                        <td style="width:130px;">
                                        	<input type="submit" style="width:100px;" name="Search" class="btnupbg btnupload" value="<?php echo $l_search;?>" />
                                        </td>
                                        <td style="width:130px;">
                                        	<input type="submit" style="width:100px;" name="SearchALL" class="btnupbg btnupload" value="<?php echo $l_all;?>" />
                                        </td>
                                    </tr>
                                </table>
							</div>
							<?php 
								if(isset($_REQUEST['Search'])){
									$search="Search";
								}else if(isset($_REQUEST['SearchALL'])){
									$search="SearchALL";
								}else{
									$search="today";
								}
							?>
							<div id="viewsearch" >
                            <div style="font-size:18px; padding-left:10px; margin-bottom:10px; font-weight:bold;"> <?php echo $l_Purchaseorder;?> : <?php echo mysql_num_rows($mapp);?></div>
                            <table class="rf-dt" width="100%">
                                <thead>																
                                    <tr>
                                      <th width="5%" class="rf-dt-shdr-c" style="padding:10px" scope="col"><?php echo $l_no;?></th>
                                      <th width="45%" class="rf-dt-shdr-c" style="padding:10px" scope="col"><a onclick="sort(<?=$page?>,'approvedoc.Tile',1,'<?=$search?>');" style="color:#1f71ee"><?php echo $l_title;?></a></th>
                                        <th width="10%" class="rf-dt-shdr-c" scope="col"><a onclick="sort(<?=$page?>,'approvedoc.SumMoney',1,'<?=$search?>');" style="color:#1f71ee"><?php echo $l_total;?></a></th>
                                        <th width="25%" class="rf-dt-shdr-c" scope="col"><a onclick="sort(<?=$page?>,'employee.FullName',1,'<?=$search?>');" style="color:#1f71ee"><?php echo $l_writer;?></a></th>
                                        <th width="15%" class="rf-dt-shdr-c" scope="col"><a onclick="sort(<?=$page?>,'approvedoc.Timecreate',1,'<?=$search?>');" style="color:#1f71ee"><?php echo $l_created_date;?></a></th>
                                    </tr>
                                </thead>
                                <tbody>
                                	<?php 
                                		if(mysql_num_rows($mapp)>0){
                                			$i=0;
                                			$tong=0;
                                			$stong=0;
                                			while($row = mysql_fetch_array($mapp)){
                                				$tien=0;
                                				if($row[SumMoney]>0){
                                        			$tien=$row[SumMoney];
                                        		}else{
                                        			$s1="select sum(buying_document.total) as tongtien from approvedoc,buying_document where approvedoc.IDApproveDoc=buying_document.document_id and approvedoc.IDApproveDoc='".$row[IDApproveDoc]."' GROUP BY approvedoc.IDApproveDoc";
													$k1=mysql_query($s1);
													while ($r1=mysql_fetch_array($k1)){
														if($r1[tongtien]>0){
															$tien=$r1[tongtien];
															$s2="update approvedoc set approvedoc.SumMoney=".$tien." where approvedoc='".$row[IDApproveDoc]."'";
															mysql_query($s2);
														}
													}
                                        		}
												if ($i < $page_end){ 
													if ( $i >= $page_start ){
	                                				?>
	                                				<tr>
				                                      <td class="rf-dt-c"><?=$i+1?></td>
				                                        <td class="rf-dt-c"><a href="accountant-search.php?id=<?=$row[IDApproveDoc]?>&type=<?=$row[IDTypeDoc]?>&page=<?=$page?>" style="color:#0078D0"><?php echo $row[Tile]?></a></td>
				                                        <td class="rf-dt-c" style="padding:10px;text-align: right;">
				                                        	<?php 
				                                        		echo number_format($tien);
				                                        	?>
				                                        </td>
				                                        <td class="rf-dt-c" style="text-align: center;"><?php echo $row[FullName]?></td>
				                                        <td class="rf-dt-c" style="text-align: center;"><?php echo $row[Timecreate]?></td>
				                                    </tr>
	                                				<?php 
	                                					$tong +=$tien;
													}
		                                			$i++; 
												}
												$stong +=$tien;
                                			}
                                			?>
                                			<tr>
                                				<td colspan="2" class="rf-dt-shdr-c" style="padding:10px;text-align: right;" scope="col"><?php echo $l_total;?></td>
                                				<td class="rf-dt-shdr-c" style="padding:10px;text-align: right;" scope="col"><?php echo number_format($tong);?></td>
                                				<td colspan="2" class="rf-dt-shdr-c" style="padding:10px" scope="col"></td>
                                			</tr>
                                			<tr>
                                				<td colspan="2" class="rf-dt-shdr-c" style="padding:10px;text-align: right;" scope="col"><?php echo $l_Purchasetotal;?></td>
                                				<td class="rf-dt-shdr-c" style="padding:10px;text-align: right;" scope="col"><?php echo number_format($stong);?></td>
                                				<td colspan="2" class="rf-dt-shdr-c" style="padding:10px" scope="col"></td>
                                			</tr>
                                			<?php 
                                		}
                                		
                                	?>
											<?php if ( $number_of_page > 1 ) 
											{ ?>
											<tr>
                                				<td colspan="5" class="rf-dt-shdr-c" style="padding:10px" scope="col" >
													<span style="background:#fff; padding:1px; display: inline-block;">
											<?php 
												if ($page==1)
													$p=2;
												else if ($page==$number_of_page)
													$p=$number_of_page-1;
												else $p=$page;
												if($page!=1){?>
													<a onclick="sort(1,'<?=$str?>',0),'<?=$search?>';" ><span class='button_next'><?php echo "<<<";?></span> </a>
													<a onclick="sort(<?=($page-1)?>,'<?=$str?>',0,'<?=$search?>');" ><span class='button_next'><?php echo "<<";?></span> </a>
												<?php  }
												else{
													echo "<span class='button_next'><<<</span> "; 
													echo "<span class='button_next'><<</span> ";
												}
												for ( $i = $p-1; $i <= $p+1; $i++ ) {	
														if ($i<=$number_of_page && $i>0)
														
															if ($i==$page){
																echo "<span class='button_next' style='color:black;';><b>".$i."</b></span>"; 
															}else{
															?>
																<a onclick="sort(<?=$i?>,'<?=$str?>',0,'<?=$search?>');" ><span class="button_next" style="color: #508e21;">
																<?php 
																echo $i;
																?>
																</span> </a>
																<?php 
															}
														?>
													
												<?php 
														}
													
													if($page!=$number_of_page){ ?>
														<a onclick="sort(<?=($page+1)?>,'<?=$str?>',0,'<?=$search?>');"><span class='button_next'><?php echo ">>";?></span> </a>
														<a onclick="sort(<?=($number_of_page)?>,'<?=$str?>',0,'<?=$search?>');" ><span class='button_next'><?php echo ">>>";?></span> </a>
													<?php }
													else{
														echo "<span class='button_next'>>></span> ";
														echo "<span class='button_next'>>>></span> ";
													}?>
														</span>
														</td>
					                                </tr>
													<?php 
												}
												?>
                                </tbody>
                            </table>
                            </div>
					  </div>
					  </form>
					</div>
